package com.ljh.blog.common.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import javax.crypto.SecretKey;
import java.util.Map;

/**
 * @author: Themberfue
 * @date: 2025/4/1 19:34
 * @description:
 */
public class JwtUtils {
    // 生成一个密钥（保证安全性）
    private static final SecretKey SECRET_KEY = Keys.hmacShaKeyFor("MySecretKeyForJwtTokenMySecretKeyForJwtToken".getBytes());

    public static String genToken(Map<String, Object> claims) {
        return Jwts.builder()
                .setClaims(claims)
                .signWith(SECRET_KEY)
                .compact();
    }

    public static Claims parseToken(String token) {
        try {
            return Jwts.parserBuilder()
                    .setSigningKey(SECRET_KEY)
                    .build()
                    .parseClaimsJws(token)
                    .getBody();
        } catch (JwtException e) {
            return null;
        }
    }
}
